package org.apache.commons.graph.export

import org.apache.commons.graph.Graph
import org.apache.commons.graph.Mapper
//import org.xml.sax.helpers.AttributesImpl
//import javax.xml.transform.TransformerFactory
//import javax.xml.transform.sax.SAXTransformerFactory
//import javax.xml.transform.sax.TransformerHandler
//
///*
//* Licensed to the Apache Software Foundation (ASF) under one
//* or more contributor license agreements.  See the NOTICE file
//* distributed with this work for additional information
//* regarding copyright ownership.  The ASF licenses this file
//* to you under the Apache License, Version 2.0 (the
//* "License"); you may not use this file except in compliance
//* with the License.  You may obtain a copy of the License at
//*
//*   http://www.apache.org/licenses/LICENSE-2.0
//*
//* Unless required by applicable law or agreed to in writing,
//* software distributed under the License is distributed on an
//* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//* KIND, either express or implied.  See the License for the
//* specific language governing permissions and limitations
//* under the License.
//*/
//
//class GraphMLExporter<V, E>(
//    graph: Graph<V, E>,
//    name: String?
//) : AbstractExporter<V, E, GraphMLExporter<V, E>>(graph, name) {
//
//    private var transformerHandler: TransformerHandler? = null
//
//    @Throws(Exception::class)
//    override fun comment(text: String) {
//        transformerHandler!!.comment(text.toCharArray(), 0, text.length)
//    }
//
//    @Throws(Exception::class)
//    override fun edge(edge: E, head: V, tail: V, properties: Map<String, Any>?) {
//        val atts = AttributesImpl()
//        atts.addAttribute(GRAPHML_XMLNS, ID, ID, CDATA_TYPE, edge.hashCode().toString())
//        atts.addAttribute(
//            GRAPHML_XMLNS, SOURCE, SOURCE, CDATA_TYPE, graph.getVertices(
//                edge
//            )!!.head.hashCode().toString()
//        )
//        atts.addAttribute(
//            GRAPHML_XMLNS, TARGET, TARGET, CDATA_TYPE, graph.getVertices(
//                edge
//            )!!.tail.hashCode().toString()
//        )
//        transformerHandler!!.startElement(GRAPHML_XMLNS, EDGE, EDGE, atts)
//
//        // TODO print properties
//        transformerHandler!!.endElement(GRAPHML_XMLNS, NODE, NODE)
//    }
//
//    @Throws(Exception::class)
//    override fun endGraph() {
//        transformerHandler!!.endElement(GRAPHML_XMLNS, GRAPH, GRAPH) // graph
//        transformerHandler!!.endElement(GRAPHML_XMLNS, GRAPHML, GRAPHML) // graphml
//    }
//
//    @Throws(Exception::class)
//    override fun endSerialization() {
//        transformerHandler!!.endDocument()
//        writer!!.flush()
//        writer!!.close()
//    }
//
//    @Throws(Exception::class)
//    override fun enlistEdgesProperty(name: String?, type: Class<*>?) {
//        enlistProperty(name, type, EDGE)
//    }
//
//    @Throws(Exception::class)
//    private fun enlistProperty(name: String?, type: Class<*>?, element: String) {
//        val atts = AttributesImpl()
//        atts.addAttribute(GRAPHML_XMLNS, ID, ID, CDATA_TYPE, name)
//        atts.addAttribute(GRAPHML_XMLNS, FOR, FOR, CDATA_TYPE, element)
//        atts.addAttribute(GRAPHML_XMLNS, ATTR_NAME, ATTR_NAME, CDATA_TYPE, name)
//        atts.addAttribute(GRAPHML_XMLNS, ATTR_TYPE, ATTR_TYPE, CDATA_TYPE, getStringType(type!!))
//        transformerHandler!!.startElement(GRAPHML_XMLNS, KEY, KEY, atts)
//        transformerHandler!!.endElement(GRAPHML_XMLNS, KEY, KEY)
//    }
//
//    @Throws(Exception::class)
//    override fun enlistVerticesProperty(name: String?, type: Class<*>?) {
//        enlistProperty(name, type, NODE)
//    }
//
//    @Throws(Exception::class)
//    override fun startGraph(name: String?) {
//        transformerHandler!!.startElement(GRAPHML_XMLNS, GRAPHML, GRAPHML, AttributesImpl())
//
//        val atts = AttributesImpl()
//        atts.addAttribute(GRAPHML_XMLNS, ID, ID, CDATA_TYPE, name)
//        atts.addAttribute(GRAPHML_XMLNS, EDGEDEFAULT, EDGEDEFAULT, CDATA_TYPE, DIRECTED)
//        transformerHandler!!.startElement(GRAPHML_XMLNS, GRAPH, GRAPH, atts)
//    }
//
//    @Throws(Exception::class)
//    override fun startSerialization() {
//        transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler()
//        //transformerHandler!!.setResult(StreamResult(writer))
//        transformerHandler!!.startDocument()
//    }
//
//    @Throws(Exception::class)
//    override fun vertex(vertex: V, properties: Map<String, Any>?) {
//        val atts = AttributesImpl()
//        atts.addAttribute(GRAPHML_XMLNS, ID, ID, CDATA_TYPE, vertex.hashCode().toString())
//        transformerHandler!!.startElement(GRAPHML_XMLNS, NODE, NODE, atts)
//
//        // TODO print properties
//        transformerHandler!!.endElement(GRAPHML_XMLNS, NODE, NODE)
//    }
//
//    fun withEdgeLabels(edgeLabels: Mapper<E, String>): GraphMLExporter<V, E> {
//        super.addEdgeProperty(LABEL, edgeLabels)
//        return this
//    }
//
//    fun <N : Number?> withEdgeWeights(edgeWeights: Mapper<E, N>): GraphMLExporter<V, E> {
//        super.addEdgeProperty(WEIGHT, edgeWeights)
//        return this
//    }
//
//    fun withVertexLabels(vertexLabels: Mapper<V, String>): GraphMLExporter<V, E> {
//        super.addVertexProperty(LABEL, vertexLabels)
//        return this
//    }
//
//    fun <N : Number?> withVertexWeights(vertexWeights: Mapper<V, N>): GraphMLExporter<V, E> {
//        super.addVertexProperty(WEIGHT, vertexWeights)
//        return this
//    }
//
//    companion object {
//        private val SAX_TRANSFORMER_FACTORY = TransformerFactory.newInstance() as SAXTransformerFactory
//
//        private const val GRAPHML = "graphml"
//
//        private const val XMLNS = "xmlns"
//
//        private const val GRAPHML_XMLNS = "http://graphml.graphdrawing.org/xmlns"
//
//        private const val EDGEDEFAULT = "edgedefault"
//
//        private const val DIRECTED = "directed"
//
//        private const val KEY = "key"
//
//        private const val FOR = "for"
//
//        private const val ID = "id"
//
//        private const val ATTR_NAME = "attr.name"
//
//        private const val ATTR_TYPE = "attr.type"
//
//        private const val GRAPH = "graph"
//
//        private const val NODE = "node"
//
//        private const val EDGE = "edge"
//
//        private const val SOURCE = "source"
//
//        private const val TARGET = "target"
//
//        private const val DATA = "data"
//
//        private const val LABEL = "label"
//
//        private const val STRING = "string"
//
//        private const val FLOAT = "float"
//
//        private const val DOUBLE = "double"
//
//        private const val LONG = "long"
//
//        private const val BOOLEAN = "boolean"
//
//        private const val INT = "int"
//
//        private const val WEIGHT = "weight"
//
//        private const val CDATA_TYPE = "CDATA"
//
//        private fun <T> getStringType(type: Class<T>): String {
//            if (Int::class.java == type) {
//                return INT
//            } else if (Long::class.java == type) {
//                return LONG
//            } else if (Float::class.java == type) {
//                return FLOAT
//            } else if (Double::class.java == type) {
//                return DOUBLE
//            } else if (Boolean::class.java == type) {
//                return BOOLEAN
//            }
//            return STRING
//        }
//    }
//}
